package com.example.grpc.openapi.response;

import java.io.Serializable;
import java.util.Objects;

public class ResponseResult<T> implements Serializable {
    private static final long serialVersionUID = -4011251291260953701L;
    public static final String SUCCESS_CODE = "00000";
    public static final String SUCCESS_MSG = "success";
    public static final String FAIL_CODE_FILL = "-1";
    /**
     * 响应编码
     */
    private final String code;
    /**
     * 响应信息
     */
    private final String msg;
    /**
     * 详细信息
     */
    private final String subMsg;
    /**
     * 响应数据
     */
    private final T data;

    /**
     * 调用成功
     */
    public static <T> ResponseResult<T> success() {
        return success(null);
    }

    /**
     * 调用成功
     */
    public static <T> ResponseResult<T> success(T data) {
        return new ResponseResult<>(SUCCESS_CODE, SUCCESS_MSG, null, data);
    }

    /**
     * 调用失败<br/>code: -1
     */
    public static <T> ResponseResult<T> fail(String msg) {
        return fail(msg, null);
    }

    /**
     * 调用失败<br/>code: -1
     */
    public static <T> ResponseResult<T> fail(String msg, String subMsg) {
        return fail(null, msg, subMsg);
    }

    /**
     * 调用失败
     */
    public static <T> ResponseResult<T> fail(String code, String msg, String subMsg) {
        return new ResponseResult<>(code == null ? FAIL_CODE_FILL : code, msg, subMsg, null);
    }

    private ResponseResult(String code, String msg, String subMsg, T data) {
        this.code = code;
        this.msg = msg;
        this.subMsg = subMsg;
        this.data = data;
    }

    public String getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public String getSubMsg() {
        return subMsg;
    }

    public T getData() {
        return data;
    }

    public boolean isSuccess() {
        return Objects.equals(SUCCESS_CODE, getCode());
    }

    @Override
    public String toString() {
        return "ResponseResult{" +
                "code='" + code + '\'' +
                ", msg='" + msg + '\'' +
                ", subMsg='" + subMsg + '\'' +
                ", data=" + data +
                '}';
    }
}
